﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LightSO.CMS.Model.EasyUI
{
    public class Datagrid : ModelBase
    {
        /// <summary>
        /// 构造方法
        /// </summary>
        protected Datagrid(){
            Columns = new Dictionary<string, Column>();
            SingleSelect = true;
        }

        public static Datagrid Create(dynamic ViewBag)
        {
            var grid = new Datagrid();

            if (ViewBag != null)
                ViewBag.Datagrid = grid;

            return grid;
        }

        public void Add(Column column)
        {
            if (column == null || string.IsNullOrWhiteSpace(column.Id))
                return;

            Columns.Add(column.Id, column);
        }

        /// <summary>
        /// 字段列表
        /// </summary>
        public Dictionary<string, Column> Columns { get; set; }

        /// <summary>
        /// 是否单选模式
        /// </summary>
        public bool SingleSelect { get; set; }
        /// <summary>
        /// 引用脚本路径
        /// </summary>
        public string ScriptSrc { get; set; }

        /// <summary>
        /// 创建表格，不需要默认创建工具条
        /// </summary>
        public bool IsNoNeedToolBar
        {
            get;
            set;
        }

        /// <summary>
        /// 行内双击事件（执行的Javascript脚本）
        /// </summary>
        public string OnDblClickRow { get; set; }


        static Dictionary<string, string> ColumnSections;

        static Datagrid()
        {
            ColumnSections = new Dictionary<string, string>();
        }


        /// <summary>
        /// 多表格时确保字段名不重复
        /// </summary>
        /// <param name="columnName"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static string EnsureColumnName(string pageName, string columnName, string tableName = "")
        {
            var name = string.Format("{0}_{1}", pageName, columnName);
            if (!Datagrid.ColumnSections.ContainsKey(name))
                return columnName;

            if (!Datagrid.ColumnSections.ContainsKey(string.Format("{0}_{1}", name, tableName)))
                return columnName + tableName;

            return string.Empty;
        }
    }
}